package com.uchengwang.ui.cm_ui.uu.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.cm_ui.uu.bean.RyGroupInviteBean;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/10/22.
 *
 */

public class RyGroupInviteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<RyGroupInviteBean> list = new ArrayList<>();
    private int type;//1申请 2邀请

    public RyGroupInviteAdapter(Context context, int type) {
        this.context = context;
        this.type = type;
    }

    public void setData(List<RyGroupInviteBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public List<RyGroupInviteBean> getData() {
        return list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_invite_group, parent, false);
        return new GroupInviteVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        GroupInviteVH holder = (GroupInviteVH) h;
        final RyGroupInviteBean bean = list.get(position);
        String userName = bean.getFromNickName();
        String headLogo = bean.getFromUserAvatar();
        String groupName = bean.getGroupName();

        holder.invite_name.setText(userName);

        ImageManager.getInstance().loadImage(context, holder.invite_head, headLogo, ImageManager.ImageType.CIRCLE);

        switch (type){//1申请 2邀请
            case 1://别人申请加群
                holder.invite_text.setText("申请加入");
                holder.invite_groupName.setText(groupName);
                switch (bean.getApplyStatus()){//0=未读 1=已读 2=接受 3=不接受
                    case 0:
                    case 1:
//                        holder.invite_cancel.setVisibility(View.VISIBLE);
//                        holder.invite_divider.setVisibility(View.VISIBLE);
                        holder.invite_cancel.setText("忽略");
                        holder.invite_ok.setText("同意");
                        holder.invite_ok.setEnabled(true);
                        break;
                    case 2:
//                        holder.invite_cancel.setVisibility(View.GONE);
//                        holder.invite_divider.setVisibility(View.GONE);
                        holder.invite_ok.setText("已同意");
                        holder.invite_ok.setEnabled(false);
                        break;
                    case 3:
//                        holder.invite_cancel.setVisibility(View.GONE);
//                        holder.invite_divider.setVisibility(View.GONE);
                        holder.invite_ok.setText("已拒绝");
                        holder.invite_ok.setEnabled(false);
                        break;
                }

                break;
            case 2://别人邀请你加群
                holder.invite_text.setText("邀请您加入");
                holder.invite_groupName.setText(groupName);
                switch (bean.getInviteStatus()){//0=未读 1=已读 2=接受 3=不接受
                    case 0:
                    case 1:
//                        holder.invite_cancel.setVisibility(View.VISIBLE);
//                        holder.invite_divider.setVisibility(View.VISIBLE);
                        holder.invite_cancel.setText("忽略");
                        holder.invite_ok.setText("同意");
                        holder.invite_ok.setEnabled(true);
                        break;
                    case 2:
//                        holder.invite_cancel.setVisibility(View.GONE);
//                        holder.invite_divider.setVisibility(View.GONE);
                        holder.invite_ok.setText("已加入");
                        holder.invite_ok.setEnabled(false);
                        break;
                    case 3:
//                        holder.invite_cancel.setVisibility(View.GONE);
//                        holder.invite_divider.setVisibility(View.GONE);
                        holder.invite_ok.setText("已拒绝");
                        holder.invite_ok.setEnabled(false);
                        break;
                }
                break;
        }

        holder.invite_cancel.setVisibility(View.GONE);
        holder.invite_divider.setVisibility(View.GONE);


        holder.invite_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RyGroupManager.getManager().conductGroupNotify((Activity) context, type == 2, bean.getId(), true, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            if (type ==2){//1申请 2邀请
                                bean.setInviteStatus(2);
                            }else {
                                bean.setApplyStatus(2);
                            }
                            notifyDataSetChanged();
                        }else {
                            UCommonUtil.getUtil().showToast(context, msg);
                        }
                    }
                });

            }
        });

        holder.invite_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RyGroupManager.getManager().conductGroupNotify((Activity) context, type == 2, bean.getId(), false, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            if (type ==2){//1申请 2邀请
                                bean.setInviteStatus(3);
                            }else {
                                bean.setApplyStatus(3);
                            }
                            notifyDataSetChanged();
                        }
                    }
                });
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class GroupInviteVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView invite_head;
        private TextView invite_name, invite_groupName, invite_divider, invite_text;
        private Button invite_cancel, invite_ok;

        public GroupInviteVH(View view) {
            super(view);
            AutoUtils.autoSize(view);
            itemView = view;

            invite_head = (ImageView) view.findViewById(R.id.invite_head);
            invite_name = (TextView) view.findViewById(R.id.invite_name);
            invite_text = (TextView) view.findViewById(R.id.invite_text);
            invite_groupName = (TextView) view.findViewById(R.id.invite_groupName);
            invite_divider = (TextView) view.findViewById(R.id.invite_divider);

            invite_cancel = (Button) view.findViewById(R.id.invite_cancel);
            invite_ok = (Button) view.findViewById(R.id.invite_ok);
        }
    }


}
